#isNull
execute if data entity @s item.components."minecraft:custom_data".VoidShopSlot{isNull:true} run return run data remove entity @n[tag=VoidShop.slotInteraction,type=interaction,distance=..1] interaction

execute as @n[tag=VoidShop.slotInteraction,type=interaction,distance=..1] on target run tag @s add buyer
tag @s add seller

#buyCondition
execute if data entity @s item.components."minecraft:custom_data".VoidShopSlot.Item.buy_condition store result score #conditionTest temp run function vs:system/game_ctrol/void_shop/trade/buy_condition_test with entity @s item.components."minecraft:custom_data".VoidShopSlot.Item.buy_condition
execute if score #conditionTest temp matches 1.. run scoreboard players reset #conditionTest temp
execute if score #conditionTest temp matches ..0 as @p[tag=buyer] at @s run return run function vs:system/game_ctrol/void_shop/trade/buy_fail/condition_test_fail

execute unless function vs:system/game_ctrol/void_shop/trade/buy_condition/is_ally as @p[tag=buyer] run return run function vs:system/game_ctrol/void_shop/trade/buy_fail/not_ally

##Buy
$execute if data entity @n[tag=VoidShop,distance=..10,type=marker] data.shopContents[$(Index)].inventory store result score #inventory temp run data get entity @n[tag=VoidShop,distance=..10,type=marker] data.shopContents[$(Index)].inventory


execute store result score #voidogenCount temp run clear @p[tag=buyer] *[custom_data~{id:"voidogen"}] 0
execute store result score #needVoidogenCount temp run data get entity @s item.components."minecraft:custom_data".void_shop.price
execute if score #voidogenCount temp >= #needVoidogenCount temp run tag @p[tag=buyer] add buy_success
tag @p[tag=buyer,tag=!buy_success,gamemode=creative] add buy_success

data modify storage vs:system temp.VSTrade set from entity @s item.components."minecraft:custom_data".VoidShopSlot.Item
execute if entity @p[tag=buy_success] store result storage vs:system temp.VSTrade.needCount int 1 run data get entity @s item.components."minecraft:custom_data".void_shop.price
execute if entity @p[tag=buy_success] if data entity @s item.components."minecraft:custom_data".void_shop.custom_price store result storage vs:system temp.VSTrade.needCount int 1 run data get entity @s item.components."minecraft:custom_data".void_shop.void_shop.custom_price
execute if entity @p[tag=buy_success] unless data entity @s item.components."minecraft:custom_data".VoidShopSlot.Item.buy_loot_table run data modify storage vs:system temp.VSTrade.loot_table set from entity @s item.components."minecraft:custom_data".VoidShopSlot.Item.loot_table
execute if entity @p[tag=buy_success] if data entity @s item.components."minecraft:custom_data".VoidShopSlot.Item.buy_loot_table run data modify storage vs:system temp.VSTrade.loot_table set from entity @s item.components."minecraft:custom_data".VoidShopSlot.Item.buy_loot_table
execute if data entity @s item.components."minecraft:custom_data".VoidShopSlot.Item.item_modifier as @p[tag=buy_success] at @s run function vs:system/game_ctrol/void_shop/trade/receive_apply_item_modifier with storage vs:system temp.VSTrade
execute unless data entity @s item.components."minecraft:custom_data".VoidShopSlot.Item.item_modifier as @p[tag=buy_success] at @s run function vs:system/game_ctrol/void_shop/trade/receive with storage vs:system temp.VSTrade

execute as @p[tag=!buy_success,tag=buyer] at @s run function vs:system/game_ctrol/void_shop/trade/buy_fail/no_money

#Extra Custom Command Events
    #BuySuccessPlayerRun
    execute if entity @p[tag=buy_success] if data entity @s item.components."minecraft:custom_data".VoidShopSlot.Item.event.buySuccessPlayerRun as @p[tag=buy_success] at @s run function vs:system/game_ctrol/void_shop/trade/events/buy_success_player_run with entity @n[tag=seller,type=item_display] item.components."minecraft:custom_data".VoidShopSlot.Item.event
    #BuySuccessReplaceTo
    execute if entity @p[tag=buy_success] if data entity @s item.components."minecraft:custom_data".VoidShopSlot.Item.event.buySuccessReplaceTo run function vs:system/game_ctrol/void_shop/trade/events/buy_success_replace_to with entity @s item.components."minecraft:custom_data".VoidShopSlot

$execute if entity @p[tag=buy_success] if score #inventory temp matches -2147483648..2147483647 store result entity @n[tag=VoidShop,distance=..10,type=marker] data.shopContents[$(Index)].inventory int 1 run scoreboard players remove #inventory temp 1
$execute if score #inventory temp matches ..0 run function vs:system/game_ctrol/void_shop/trade/sold {index:$(Index)}

tag @p[tag=buyer] remove buyer
tag @s remove seller
tag @p[tag=buy_success] remove buy_success
scoreboard players reset #inventory temp
scoreboard players reset #voidogenCount temp
scoreboard players reset #needVoidogenCount temp
data remove storage vs:system temp.VSTrade
data remove entity @s item.components."minecraft:custom_data".void_shop.custom_price
data remove entity @n[tag=VoidShop.slotInteraction,type=interaction,distance=..1] interaction